import get from '@dfeidao/atom-web/local/get';
import set from '@dfeidao/atom-web/local/set';
import nodejs from '@dfeidao/atom-web/msg/nodejs';
import render from '@dfeidao/atom-web/render/render';
import Tree from '@dfeidao/fd-w000001';
import { IFeidaoAiBrowserComponent } from '@dfeidao/web/interfaces';

import s_get from '@dfeidao/atom-web/storage/get';
// 重新渲染树形图
import P01 from './P01';
import P04 from './P04';
export default async function a003(fd: IFeidaoAiBrowserComponent) {

	const productid = await s_get('productid');

	const client_type = fd.data.node.querySelector<HTMLSelectElement>('[data-feidao-id="client_style"]').value;
	set(fd, 'client_type', client_type);
	// console.log(client_type)
	const formgroup_no = get(fd, 'no1');
	// console.log(client_type, 'pppp')
	set(fd, 'client_type1', client_type);
	if (formgroup_no) {
		const res = await nodejs<Array<{ menu_no: string, menu_name: string, parent_no: string, _id: string, checked: boolean }>>('functional-permissions/zj-000002/s003', { productid, formgroup_no, client_type });
		set(fd, 'res', res);
		if (res.length === 0) {
			render(fd, '', P01, 'P01', 'inner');
			render(fd, '', P04, 'P04', 'inner');
			set(fd, 'checked1', productid);
			set(fd, 'checked2', client_type);
			if (!get(fd, 'client_type')) {
				set(fd, 'client_type3', productid);
			}

		} else {
			const cdata = res.map((d) => {
				return { id: d.menu_no, text: d.menu_name, p_id: d.parent_no, _id: d._id };
			});
			render(fd, JSON.stringify(cdata), P01, 'P01', 'inner');
			fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P04"]').innerHTML = '';
			set(fd, 'checked1', productid);
			set(fd, 'checked2', client_type);
		}
		const checked = new Array();
		res.forEach((item) => {
			if (item.checked === true) {
				checked.push(item.menu_no);
			}

		});

		const widget: Tree = document.querySelector('#widget');
		widget.check_node(checked);
		// console.log(checked)

	} else {
		const res = await nodejs<Array<{ menu_no: string, menu_name: string, parent_no: string, _id: string, checked: boolean }>>('functional-permissions/zj-000002/s003', { productid, formgroup_no, client_type });
		set(fd, 'res', res);
		if (res.length === 0) {
			render(fd, '', P01, 'P01', 'inner');
			render(fd, '', P04, 'P04', 'inner');
			set(fd, 'checked1', productid);
			set(fd, 'checked2', client_type);

		} else {
			const cdata = res.map((d) => {
				return { id: d.menu_no, text: d.menu_name, p_id: d.parent_no, _id: d._id };
			});
			render(fd, JSON.stringify(cdata), P01, 'P01', 'inner');
			fd.data.node.querySelector<HTMLDivElement>('[data-feidao-presentation="P04"]').innerHTML = '';
		}
		set(fd, 'checked1', productid);
		set(fd, 'checked2', productid);
	}

}
